package com.mybatisplus.controller;

import com.mybatisplus.entity.Student;
import com.mybatisplus.entity.Teacher;
import com.mybatisplus.entity.UserInfo;
import com.mybatisplus.service.LoginService;
import com.mybatisplus.service.StudentService;
import com.mybatisplus.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Auto created By IntelliJ IDEA
 *
 * @author : gzrong
 * @Date : 2023/5/611:00
 */
@RestController
@RequestMapping("/user")
public class UserController {
    SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd号 HH:mm:ss");
    @Autowired
    private LoginService loginService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private TeacherService teacherService;

    //登录请求方法
    @RequestMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        int usertype = Integer.parseInt(request.getParameter("usertype"));
        boolean login = loginService.login(request.getParameter("stuid"), request.getParameter("pwd"), usertype, response);

        if (login) {
            UserInfo userInfo = new UserInfo();
            switch (usertype) {
                case 1: {
                    Teacher teacher = teacherService.selectTeacherById(request.getParameter("stuid"));
                    HttpSession session = request.getSession();
                    System.out.println(format.format(new Date()));
                    System.out.println("用户登录请求" +
                            "\n本次会话session:" + request.getSession().getId() +
                            "\n账号:" + teacher.getTeacherID() +
                            "\n姓名:" + teacher.getName() +
                            "\n类别:" + request.getParameter("usertype"));
                    userInfo.setUserType(request.getParameter("usertype"));
                    userInfo.setUserId(teacher.getTeacherID());
                    userInfo.setUsername(teacher.getName());
                    userInfo.setStatus(true);
                    userInfo.setCurrentPage("0");
                    session.setAttribute("userinfo", userInfo);
                    session.setMaxInactiveInterval(900);
                    break;
                }
                case 2: {
                    Student student = studentService.selectStudentByStuid(request.getParameter("stuid"));
                    HttpSession session = request.getSession();
                    System.out.println(format.format(new Date()));
                    System.out.println("用户登录请求" +
                            "\n本次会话session:" + request.getSession().getId() +
                            "\n账号:" + student.getStuid() +
                            "\n姓名:" + student.getName() +
                            "\n类别:" + request.getParameter("usertype"));
                    userInfo.setUsername(student.getName());
                    userInfo.setUserId(student.getStuid());
                    userInfo.setStatus(true);
                    userInfo.setUserType(request.getParameter("usertype"));
                    userInfo.setCurrentPage("0");
                    session.setAttribute("userinfo", userInfo);
                    session.setAttribute("student", student);
                    session.setMaxInactiveInterval(900);
                    break;
                }
            }
        }
    }

    //注销请求方法
    @RequestMapping("logOut")
    public void logOut(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        if (session != null) {
            System.out.println(format.format(new Date()));
            System.out.println(((UserInfo) session.getAttribute("userinfo")).getUsername() + "已退出登录");
            System.out.println("正在摧毁Session=>" + session.getId());
            request.getSession().invalidate();
            System.out.println("Session已摧毁");
        }
    }

    //获取用户状态信息方法
    @RequestMapping("/userStatus")
    public UserInfo userStatus(HttpServletRequest request) {
        //前端请求检查会话信息，返回UserInfo实体类
        UserInfo userInfo = null;
        HttpSession session = request.getSession(false);
        if (session != null) {
            userInfo = (UserInfo) session.getAttribute("userinfo");
            String currentPage = request.getParameter("currentPage");
            if (currentPage != null)
                userInfo.setCurrentPage(currentPage);
        }
        return userInfo;
    }
}
